package com.example.demoresilience4jfeignconsumer;

import java.time.Duration;
import java.util.concurrent.TimeUnit;
import feign.Feign;
import feign.Request;
import io.github.resilience4j.feign.FeignDecorators;
import io.github.resilience4j.feign.Resilience4jFeign;
import io.github.resilience4j.ratelimiter.RateLimiter;
import io.github.resilience4j.ratelimiter.RateLimiterConfig;
import io.github.resilience4j.ratelimiter.RateLimiterRegistry;
import org.springframework.context.annotation.Bean;

public class Resilience4jFeignConfiguration {
    @Bean
    public Feign.Builder feignBuilder() {

//        RateLimiterConfig config = RateLimiterConfig.custom()
//                .limitRefreshPeriod(Duration.ofMillis(5000))
//                .limitForPeriod(1)
//                .timeoutDuration(Duration.ofMillis(6000))
//                .build();
//        RateLimiter rateLimiter = RateLimiter.of("backendB", config);
//        CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("backendName");
//        FeignDecorators decorators = FeignDecorators.builder()
//                .withRateLimiter(rateLimiter)
//                .withCircuitBreaker(circuitBreaker)
//                .build();

        FeignDecorators decorators =
        FeignDecorators.builder().withFallbackFactory(FeignFallBack::new).build();
        return Resilience4jFeign.builder(decorators)
                .requestInterceptor(new Resllience4jFeigniRequestInterceptor())
                .options(new Request.Options(200, TimeUnit.MILLISECONDS, 200, TimeUnit.MILLISECONDS, true));
    }
}
